package com.future.mayi.activity;

import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.future.baselib.entity.DefaultResponse;
import com.future.baselib.utils.ApiCst;
import com.future.baselib.utils.HttpRequest;
import com.future.mayi.R;
import com.future.mayi.app.App;
import com.future.mayi.model.BankModel;
import com.future.mayi.model.WalletModel;
import com.future.mayi.response.WalletResponse;
import com.future.mayi.utils.HCUtils;
import com.future.mayi.utils.PriceUtils;

import butterknife.BindView;
import butterknife.OnClick;

public class WithdrawActivitys extends BaseTitleActivity {
    @BindView(R.id.tv_add_card)
    TextView tvAddCard;
    @BindView(R.id.edt_num)
    EditText edtNum;
    @BindView(R.id.tv_enter)
    TextView tvEnter;
    @BindView(R.id.iv_bank_icon)
    ImageView ivBankIcon;
    @BindView(R.id.tv_bank_name)
    TextView tvBankName;
    @BindView(R.id.tv_bank_card)
    TextView tvBankCard;
    @BindView(R.id.tv_max_num)
    TextView tvMaxNum;
    @BindView(R.id.tv_min)
    TextView tvMin;
    @BindView(R.id.rl_card)
    RelativeLayout rlCard;


    private WalletModel model;

    @Override
    protected void initTopBar() {
        initBack();
        getTitleText().setText("提现");
    }

    @Override
    protected void initLayout() {
        setContentView(R.layout.activity_withdraws);
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initDates() {

        getData();

    }

    @Override
    protected void initListener() {

    }


    private void getData() {
        showLoadingDialog();
        new HttpRequest<WalletResponse>()
                .with(this).setApiCode(ApiCst.getBankInfo)
                .addParam("token", App.getToken(mContext))
                .setListener(new HttpRequest.OnNetworkListener<WalletResponse>() {
                    @Override
                    public void onSuccess(WalletResponse response) {
                        dissmissLoadingDialog();
                        model = response.model;
                        setBankView();
                    }

                    @Override
                    public void onFail(String message, WalletResponse response) {
                        dissmissLoadingDialog();
                        toast(message);
                        finish();
                    }
                }).start(new WalletResponse());
    }

    private static final int AND_BANK_CARD_ACTIVITY_CODE = 101;

    @OnClick({R.id.tv_add_card, R.id.tv_enter, R.id.rl_card})
    public void onViewClicked(View view) {
        if (model == null) return;
        switch (view.getId()) {
            case R.id.rl_card:
            case R.id.tv_add_card:
                Intent intent = new Intent(this, AndBankCardActivity.class);
                if (model.getBank() != null)
                    intent.putExtra("bankCardModel", model.getBank());
                startActivityForResult(intent, AND_BANK_CARD_ACTIVITY_CODE);
                break;
            case R.id.tv_enter:
                tvEnter.setEnabled(false);
                if (model.getBank() == null || TextUtils.isEmpty(model.getBank().getBank_name()) || model.getBank().getUser_bank_id() <= 0) {
                    toast("请添加银行卡");
                    tvEnter.setEnabled(true);
                    return;
                }

                if (TextUtils.isEmpty(edtNum.getText())) {
                    toast("请输入提现金额");
                    tvEnter.setEnabled(true);
                    return;
                }


                String prices = edtNum.getText().toString();
                String pricess = edtNum.getText().toString().replaceAll("\\.", "");
                int size = prices.length() - pricess.length();
                if (size > 1) {
                    toast("请输入正确的价格");
                    tvEnter.setEnabled(true);
                    return;
                }

                double price = Double.valueOf(prices);

                if (price < model.getMin_cash()) {
                    toast("最小提现金额为" + PriceUtils.getPriceText(model.getMin_cash()) + "元");
                    tvEnter.setEnabled(true);
                    return;
                }

                if (price > model.getCash_price()) {
                    toast("超出最大提现金额");
                    tvEnter.setEnabled(true);
                    return;
                }

                enter(PriceUtils.getPriceText(price));


                break;
        }
    }

    private void enter(String price) {
        showLoadingDialog();
        new HttpRequest<DefaultResponse>()
                .with(this).setApiCode(ApiCst.addCashWallet)
                .addParam("token", App.getToken(mContext))
                .addParam("cash_price", price)
                .addParam("user_bank_id", model.getBank().getUser_bank_id())
                .setListener(new HttpRequest.OnNetworkListener<DefaultResponse>() {
                    @Override
                    public void onSuccess(DefaultResponse response) {
                        dissmissLoadingDialog();
                        startActivity(WithdrawOkActivity.class);
                        finish();
                    }

                    @Override
                    public void onFail(String message, DefaultResponse response) {
                        toast(message);
                        tvEnter.setEnabled(true);
                        dissmissLoadingDialog();
                    }
                }).start(new DefaultResponse());
    }


    private void addBank(BankModel bankModel) {
        if (bankModel == null) return;
        showLoadingDialog();
        model.getBank().setBank_id(bankModel.getBank_id());
        model.getBank().setAccount_name(bankModel.getAccount_name());
        model.getBank().setReal_name(bankModel.getReal_name());
        new HttpRequest<DefaultResponse>()
                .with(this).setApiCode(ApiCst.addOrEditBank)
                .addParam("token", App.getToken(mContext))
                .addParam("bank_id", model.getBank().getBank_id() <= 0 ? null : model.getBank().getBank_id())
                .addParam("account_name", model.getBank().getAccount_name())
                .addParam("real_name", model.getBank().getReal_name())
                .addParam("user_bank_id", model.getBank().getUser_bank_id() <= 0 ? null : model.getBank().getUser_bank_id())
                .setListener(new HttpRequest.OnNetworkListener<DefaultResponse>() {
                    @Override
                    public void onSuccess(DefaultResponse response) {
                        dissmissLoadingDialog();
                        toast(response.message);
                        getData();
                    }

                    @Override
                    public void onFail(String message, DefaultResponse response) {
                        toast(message);
                        dissmissLoadingDialog();
                    }
                }).start(new DefaultResponse());
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {
            if (AND_BANK_CARD_ACTIVITY_CODE == requestCode && data != null) {
                BankModel bankModel = (BankModel) data.getSerializableExtra("bankModel");
                addBank(bankModel);
            }
        }
    }

    private void setBankView() {
        if (model == null) return;

        tvMin.setText(PriceUtils.getPriceText(model.getMin_cash()) + "元起提");
        tvMaxNum.setVisibility(View.VISIBLE);
        tvMaxNum.setText("可提现金额:" + PriceUtils.getPriceText(model.getCash_price()) + "元");


        if (model.getBank() == null || model.getBank().getAccount_name() == null || model.getBank().getUser_bank_id() <= 0) {
            tvAddCard.setVisibility(View.VISIBLE);
            rlCard.setVisibility(View.GONE);
            return;
        }
        tvAddCard.setVisibility(View.GONE);
        rlCard.setVisibility(View.VISIBLE);
        tvBankCard.setText("尾号:" + model.getBank().getAccount_name().substring(model.getBank().getAccount_name().length() - 4, model.getBank().getAccount_name().length()));
        tvBankName.setText(model.getBank().getBank_name());
        HCUtils.loadWebImg(this, ivBankIcon, model.getBank().getBank_logo());
    }


}
